/* 
 * jxListFiles Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * GPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxListFiles.view;

import org.jxUtils.file.type.AudioType;
import org.jxUtils.file.type.CompressType;
import org.jxUtils.file.type.DocumentType;
import org.jxUtils.file.type.ExecutableType;
import org.jxUtils.file.type.ImageType;
import org.jxUtils.file.type.SimpleTextType;
import org.jxUtils.file.type.VideoType;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxListFiles
 * @package org.jxListFiles.view
 * @date 18/04/2014 - 18:07:59
 */
public final class TypeFilterPanel extends JPanel {
	
	private static final long serialVersionUID = -1653588091648816209L;
	
	private List<String> extensions = new ArrayList<String>();
	
	private JToggleButton btnAudio;
	
	private JToggleButton btnCompress;
	
	private JToggleButton btnDocument;
	
	private JToggleButton btnExecutable;
	
	private JToggleButton btnImage;
	
	private JToggleButton btnSimpleText;
	
	private JToggleButton btnVideo;
	
	private JTextField txtExtensions;
	
	private JScrollPane scrlpnExtensions;
	
	private JList<String> lstExtensions;
	
	/**
	 * 
	 */
	public TypeFilterPanel() {
	
		initGUI();
	}
	
	private void initGUI() {
	
		// TODO: Implementar Internacionalização
		setBorder(new TitledBorder(null, "Procurar por:", TitledBorder.LEADING, TitledBorder.BELOW_TOP, null, null));
		setSize(490, 300);
		setLayout(null);
		
		add(getBtnAudio());
		add(getBtnCompress());
		add(getBtnDocument());
		add(getBtnExecutable());
		add(getBtnImage());
		add(getBtnSimpleText());
		add(getBtnVideo());
		add(getTxtExtensions());
		add(getScrlpnExtensions());
	}
	
	public JToggleButton getBtnAudio() {
	
		if(btnAudio == null) {
			btnAudio = new JToggleButton();
			btnAudio.setBounds(20, 40, 60, 45);
			// TODO: Implementar Internacionalização
			btnAudio.setToolTipText("Filtrar por Áudio");
			btnAudio.setIcon(new ImageIcon(new ImageIcon(InitFrame.class.getResource("/org/jxListFiles/resource/audio.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT)));
			btnAudio.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					
					// 
					if(btnAudio.isSelected()) {
						
						getListModel().addElement(AudioType.getAllExtensions().toString());
						extensions.addAll(AudioType.getAllExtensions());
						
					}else {
						
						getListModel().removeElement(AudioType.getAllExtensions().toString());
						extensions.removeAll(AudioType.getAllExtensions());
					}
				}
			});
		}
		return btnAudio;
	}
	
	public JToggleButton getBtnCompress() {
	
		if(btnCompress == null) {
			btnCompress = new JToggleButton();
			btnCompress.setBounds(85, 40, 60, 45);
			// TODO: Implementar Internacionalização
			btnCompress.setToolTipText("Filtrar por Compactado");
			btnCompress.setIcon(new ImageIcon(new ImageIcon(InitFrame.class.getResource("/org/jxListFiles/resource/package.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT)));
			btnCompress.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					
					// 
					if(btnCompress.isSelected()) {
						
						getListModel().addElement(CompressType.getAllExtensions().toString());
						extensions.addAll(CompressType.getAllExtensions());
						
					}else {
						
						getListModel().removeElement(CompressType.getAllExtensions().toString());
						extensions.removeAll(CompressType.getAllExtensions());
					}
				}
			});
		}
		return btnCompress;
	}
	
	public JToggleButton getBtnDocument() {
	
		if(btnDocument == null) {
			btnDocument = new JToggleButton();
			btnDocument.setBounds(150, 40, 60, 44);
			// TODO: Implementar Internacionalização
			btnDocument.setToolTipText("Filtrar por Documento");
			btnDocument.setIcon(new ImageIcon(new ImageIcon(InitFrame.class.getResource("/org/jxListFiles/resource/document.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT)));
			btnDocument.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					
					// 
					if(btnDocument.isSelected()) {
						
						getListModel().addElement(DocumentType.getAllExtensions().toString());
						extensions.addAll(DocumentType.getAllExtensions());
						
					}else {
						
						getListModel().removeElement(DocumentType.getAllExtensions().toString());
						extensions.removeAll(DocumentType.getAllExtensions());
					}
				}
			});
		}
		return btnDocument;
	}
	
	public JToggleButton getBtnExecutable() {
	
		if(btnExecutable == null) {
			btnExecutable = new JToggleButton();
			btnExecutable.setBounds(215, 40, 60, 44);
			// TODO: Implementar Internacionalização
			btnExecutable.setToolTipText("Filtrar por Executável");
			btnExecutable.setIcon(new ImageIcon(new ImageIcon(InitFrame.class.getResource("/org/jxListFiles/resource/executable.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT)));
			btnExecutable.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					
					// 
					if(btnExecutable.isSelected()) {
						
						getListModel().addElement(ExecutableType.getAllExtensions().toString());
						extensions.addAll(ExecutableType.getAllExtensions());
						
					}else {
						
						getListModel().removeElement(ExecutableType.getAllExtensions().toString());
						extensions.removeAll(ExecutableType.getAllExtensions());
					}
				}
			});
		}
		return btnExecutable;
	}
	
	public JToggleButton getBtnImage() {
	
		if(btnImage == null) {
			btnImage = new JToggleButton();
			btnImage.setBounds(280, 40, 60, 44);
			// TODO: Implementar Internacionalização
			btnImage.setToolTipText("Filtrar por Imagem");
			btnImage.setIcon(new ImageIcon(new ImageIcon(InitFrame.class.getResource("/org/jxListFiles/resource/image.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT)));
			btnImage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					
					// 
					if(btnImage.isSelected()) {
						
						getListModel().addElement(ImageType.getAllExtensions().toString());
						extensions.addAll(ImageType.getAllExtensions());
						
					}else {
						
						getListModel().removeElement(ImageType.getAllExtensions().toString());
						extensions.removeAll(ImageType.getAllExtensions());
					}
				}
			});
		}
		return btnImage;
	}
	
	public JToggleButton getBtnSimpleText() {
	
		if(btnSimpleText == null) {
			btnSimpleText = new JToggleButton();
			btnSimpleText.setBounds(345, 40, 60, 44);
			// TODO: Implementar Internacionalização
			btnSimpleText.setToolTipText("Filtrar por Texto");
			btnSimpleText.setIcon(new ImageIcon(new ImageIcon(InitFrame.class.getResource("/org/jxListFiles/resource/text.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT)));
			btnSimpleText.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					
					// 
					if(btnSimpleText.isSelected()) {
						
						getListModel().addElement(SimpleTextType.getAllExtensions().toString());
						extensions.addAll(SimpleTextType.getAllExtensions());
						
					}else {
						
						getListModel().removeElement(SimpleTextType.getAllExtensions().toString());
						extensions.removeAll(SimpleTextType.getAllExtensions());
					}
				}
			});
		}
		return btnSimpleText;
	}
	
	public JToggleButton getBtnVideo() {
	
		if(btnVideo == null) {
			btnVideo = new JToggleButton();
			btnVideo.setBounds(410, 40, 60, 44);
			// TODO: Implementar Internacionalização
			btnVideo.setToolTipText("Filtrar por Vídeo");
			btnVideo.setIcon(new ImageIcon(new ImageIcon(InitFrame.class.getResource("/org/jxListFiles/resource/video.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT)));
			btnVideo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					
					// 
					if(btnVideo.isSelected()) {
						
						getListModel().addElement(VideoType.getAllExtensions().toString());
						extensions.addAll(VideoType.getAllExtensions());
						
					}else {
						
						getListModel().removeElement(VideoType.getAllExtensions().toString());
						extensions.removeAll(VideoType.getAllExtensions());
					}
				}
			});
		}
		return btnVideo;
	}
	
	public JTextField getTxtExtensions() {
	
		if(txtExtensions == null) {
			txtExtensions = new JTextField();
			txtExtensions.setToolTipText("Informe as Extensões com Ponto (.) e separadas por Vírgula (,)");
			txtExtensions.setBounds(20, 95, 450, 30);
			txtExtensions.setColumns(10);
			txtExtensions.addKeyListener(new KeyAdapter() {
				
				@Override
				public void keyPressed(KeyEvent keyEvent) {
				
					// 
					if(keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
						
						// 
						if( !getTxtExtensions().getText().trim().isEmpty()) {
							
							// 
							String[] txtExtensionsArray = getTxtExtensions().getText().trim().toUpperCase().split(",");
							
							// 
							if(txtExtensionsArray != null && txtExtensionsArray.length > 0) {
								
								Collection<String> txtExtensions = Arrays.asList(txtExtensionsArray);
								
								getListModel().addElement(txtExtensions.toString().replaceAll("[ ]{2,}", " "));
								
								extensions.addAll(txtExtensions);
								
								getTxtExtensions().setText("");
							}
						}
					}
				}
			});
		}
		return txtExtensions;
	}
	
	public JScrollPane getScrlpnExtensions() {
	
		if(scrlpnExtensions == null) {
			scrlpnExtensions = new JScrollPane();
			scrlpnExtensions.setBounds(20, 130, 450, 160);
			scrlpnExtensions.setViewportView(getLstExtensions());
		}
		return scrlpnExtensions;
	}
	
	public JList<String> getLstExtensions() {
	
		if(lstExtensions == null) {
			lstExtensions = new JList<>(new DefaultListModel<String>());
			lstExtensions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			lstExtensions.addMouseListener(new MouseAdapter() {
				
				@Override
				public void mouseClicked(MouseEvent mouseEvent) {
					
					// 
					if(mouseEvent.getClickCount() == 2) {
						
						int index = lstExtensions.getSelectedIndex();
						
						getTxtExtensions().setText(
							lstExtensions.getSelectedValue()
								.replace(" ", "")
								.replace("[", "")
								.replace("]", "")
						);
						
						getListModel().remove(index);
					}
				}
			});
		}
		return lstExtensions;
	}
	
	/**
	 * @return listModel
	 */
	public DefaultListModel<String> getListModel() {
	
		return (DefaultListModel<String>)getLstExtensions().getModel();
	}
	
	/**
	 * @return extensions
	 */
	public List<String> getExtensions() {
	
		return extensions;
	}

	/**
	 * 
	 */
	public void clear() {
	
		getListModel().clear();
		getBtnAudio().setSelected(false);
		getBtnCompress().setSelected(false);
		getBtnDocument().setSelected(false);
		getBtnExecutable().setSelected(false);
		getBtnImage().setSelected(false);
		getBtnSimpleText().setSelected(false);
		getBtnVideo().setSelected(false);
	}
}
